package cn.mln521.test;

import cn.mln521.ioc.demo1.UserService;
import cn.mln521.ioc.demo1.UserServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

public class SpringDemo {

    /**
     * 传统方法创建
     */
    @Test
    public void demo1() {
        UserService userService = new UserServiceImpl();
        userService.sayHello();
    }

    /**
     * Spring ioc
     */
    @Test
    public void demo2() {
        //1.读取配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        //2.从spring工厂获取bean
        UserService userService = (UserService) context.getBean("userService");
        //3.执行方法
        userService.sayHello();
    }

    /**
     * 读取盘符系统中的配置文件
     */
    @Test
    public void demo3() {
        ApplicationContext context = new FileSystemXmlApplicationContext("G:\\application.xml");
        UserService userService = (UserService) context.getBean("userService");
        //3.执行方法
        userService.sayHello();
    }

    /**
     * 传统方式读取系统的配置文件
     */
    @Test
    public void demo4() {
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("application.xml"));
        UserService userService = (UserService) beanFactory.getBean("userService");
        //3.执行方法
        userService.sayHello();
    }
    /**
     * 传统方式读取盘符系统中的配置文件
     */
    @Test
    public void demo5() {
        BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("G:\\application.xml"));
        UserService userService = (UserService) beanFactory.getBean("userService");
        //3.执行方法
        userService.sayHello();
    }


}
